/*
 *  acerwificontroller.c - Acer Laptop ACPI Wifi Controller Gnome Applet
 *
 *
 *  Copyright (C) 2007      Fabio Fucci <fabio.fucci@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *  To learn more see the project home page at:
 *  http://code.google.com/p/acerwificontroller/
 *
 *  Credits:
 *	Many thanks to the acer_acpi team. See http://code.google.com/p/aceracpi
 *	Many thanks to the acerhk team. See http://www.cakey.de/acerhk/
 *	Many thanks to Stefano Pireddu and Olivier Prieur that contributed.
 *
 */
#include <panel-applet.h>
#include <gtk/gtk.h>

static gboolean ENABLED = FALSE;

static void enable(GtkWidget *image) {
	system("gksu /usr/local/bin/acerwificontroller_enable.sh");
	ENABLED = TRUE;
	gtk_widget_set_sensitive(image, ENABLED);
	g_debug("Wireless enabled");
}

static void disable(GtkWidget *image) {
	system("gksu /usr/local/bin/acerwificontroller_disable.sh");
	ENABLED = FALSE;
	gtk_widget_set_sensitive(image, ENABLED);
	g_debug("Wireless disabled");
}

static gboolean on_button_press(GtkWidget *event_box, GdkEventButton *event, gpointer data) {
	/* Ignores buttons other than the left mouse button;
	   return FALSE so the event is passed to the default handler */
	if (event->button != 1) {
		return FALSE;
	} else {
		if (event->type == GDK_BUTTON_PRESS) {
			// do_popup_menu (event_box, event);
			if (ENABLED) {
				disable(GTK_WIDGET(data));
			} else {
				enable(GTK_WIDGET(data));
			}
		}

		return TRUE;
	}
}

/*
 * This is the applet's main method.
 */
static gboolean acerwificontroller_applet_fill(PanelApplet *applet, const gchar *iid, gpointer data) {
	if (strcmp (iid, "OAFIID:AcerWifiController") != 0) {
		return FALSE;
	}

	GtkWidget *image;
	GtkWidget *event_box;

	image = gtk_image_new_from_file("/usr/share/icons/gnome/22x22/devices/network-wireless.png");

	event_box = gtk_event_box_new();

	enable(image);

	gtk_container_add (GTK_CONTAINER (event_box), image);
	g_signal_connect (G_OBJECT (event_box),
                  "button_press_event",
                  G_CALLBACK (on_button_press),
                  (gpointer) image);

	gtk_container_add(GTK_CONTAINER(applet), event_box);

	gtk_widget_show_all(GTK_WIDGET(applet));

	return TRUE;
}

PANEL_APPLET_BONOBO_FACTORY ("OAFIID:AcerWifiController_Factory",
                             PANEL_TYPE_APPLET,
                             "Acer Wifi Controller Applet",
                             "0.2",
                             acerwificontroller_applet_fill,
                             NULL);
